;(function(){
  'use strict';
  angular.module('services.faultLocation.tradeLog')
    .factory('TradeLogManageServer',tradeLogManagementServer)
    .factory('TradeLogManageSelector',tradeLogManageSelector)
    .factory('TradeLogManageQueryParam',tradeLogManagementQueryparam)
    .factory('TradeLogManageViewObj',tradeLogManageViewObj);
  function tradeLogManagementServer(Restangular){
    return {
      indexQuery : indexQuery,
      indexModify : indexModify
    };
    function indexQuery(param,callback) {
      return Restangular.all('sas/faultLocation/tradeLog/index/query').post(param).then(callback);
    }

    function indexModify(param,callback){
      return Restangular.all('sas/faultLocation/tradeLog/index/save').post(param).then(callback);
    }
  }
  tradeLogManagementServer.$inject = ['Restangular'];

  function tradeLogManageSelector(){
    var selector = {
      systems:[{name:'统一支付',code:'11'},{name:'物联网',code:'22'}]
    };
    return {
      selector : selector
    };
  }

  function tradeLogManagementQueryparam(TradeLogManageServer,TradeLogManageViewObj,CoreService,$mdDialog,CoreLoading){
    var param = {};
    var flag = {};
    return{
      initParam : initParam,
      initFlag : initFlag,
      tradeLogIndexQueryFn : tradeLogIndexQueryFn,
      setIndexObjFn : setIndexObjFn,
      addIndexObjFn : addIndexObjFn,
      deleteIndexObjFn : deleteIndexObjFn
    };

    function initParam(){
      param = {};
      return param;
    }

    function initFlag(){
      flag = {
        formIsFold : false,
        isQuery : false
      };
      return flag;
    }

    function setParam(){
      var tempParam = {};
      if(param.systemCode){
        tempParam.systemCode = param.systemCode.code;
      }else{
        tempParam.systemCode = '11';
      }
      return tempParam;
    }


    function tradeLogIndexQueryFn(){
    var tempParam = {}; 
      if(!flag.formIsFold) {
        tempParam = setParam();
        CoreLoading.setState(true);
        TradeLogManageServer.indexQuery(tempParam,function(res){
          if(res.state === '10000' && res.data.length > 0){
            TradeLogManageViewObj.setViewObj(res.data,tempParam);
            CoreLoading.setState(false);
            flag.formIsFold = !flag.formIsFold;
            flag.isQuery = true;
          } else{
            CoreLoading.setState(false);
            CoreService.dialog(res.state);
          }
        });
      }else{
        flag.formIsFold = !flag.formIsFold;
      }
    }

    function setIndexObjFn(item,$event){
      $mdDialog.show({
        controller:indexObjCtrl,
        targetEvent:$event,
        templateUrl:'views/faultLocation/tradeLog/setIndex.html'
      });


      function indexObjCtrl($scope){
        $scope.index = angular.copy(item);
        $scope.title = '指标编辑';
        $scope.isUpdate = true;
        $scope.notAllowedToChange = true;
        $scope.modify =function(){
          angular.copy($scope.index,item);
          TradeLogManageViewObj.updateIndexFn();
          $mdDialog.hide();
        };
        $scope.cancel = function(){
          $mdDialog.cancel();
        };
      }
    }

    function addIndexObjFn($event){
      var indexs = TradeLogManageViewObj.getViewObj().indexs;
      if(!indexs || indexs.length === 0){
        CoreService.dialog('指标已全部新增完毕');
        return;
      }
      $mdDialog.show({
        controller:indexObjCtrl,
        targetEvent:$event,
        templateUrl:'views/faultLocation/tradeLog/setIndex.html'
      });

      function indexObjCtrl($scope){
        $scope.title = '新增指标';
        $scope.indexs = indexs;
        $scope.modify =function(){
          TradeLogManageViewObj.updateIndexFn($scope.index);
          $mdDialog.hide();
        };
        $scope.cancel = function(){
          $mdDialog.cancel();
        };
      }
    }

    function deleteIndexObjFn($event){
      var confirm = $mdDialog.confirm()
      .parent(angular.element(document.body))
      .title('删除')
      .content('是否删除已选中指标')
      .ariaLabel('delete')
      .cancel('取消')
      .ok('确认')
      .targetEvent($event);

      $mdDialog.show(confirm).then(
        function ok(){
          TradeLogManageViewObj.updateIndexFn();
        },
        function cancel(){

        }
      );
    }

  }

  tradeLogManagementQueryparam.$inject = ['TradeLogManageServer','TradeLogManageViewObj','CoreService','$mdDialog','CoreLoading'];

  function tradeLogManageViewObj(TradeLogManageServer,CoreService,CoreLoading){
    var viewObj = {};
    return{
      getViewObj : getViewObj,
      initViewObj : initViewObj,
      setViewObj : setViewObj,
      updateIndexFn : updateIndexFn  
    };
    
    function getViewObj(){
      return viewObj;
    }

    function initViewObj(){
      viewObj = {};
      return viewObj;
    }

    function setViewObj(data,param){
      viewObj.series = [];
      viewObj.indexs = [];
      angular.forEach(data,function(item){
        if(item.displayState === 'y'){
          viewObj.series.push(item);
        }else{
          viewObj.indexs.push(item);
        }
      });
      viewObj.systemCode = param.systemCode;
      return viewObj;
    }

    function updateIndexFn(newIndex){
      var tempParam = [];
      var number;

      if(newIndex){
        angular.forEach(viewObj.indexs,function(item,key){
          if(item.indexName === newIndex.indexName){
            number = key;
          }
        });
        viewObj.indexs.splice(number,1);
        viewObj.series.push({
          systemCode:viewObj.systemCode,
          systemName:(viewObj.systemCode === '11'?'统一支付':'物联网'),
          indexName:newIndex.indexName,
          indexDisplayName:newIndex.indexDisplayName,
          displayState:'y',
          validState:'y'
        });
      }

      angular.forEach(viewObj.series,function(item){
        var indexObj = {
          systemCode:item.systemCode,
          systemName:item.systemName,
          indexName:item.indexName,
          indexDisplayName:item.indexDisplayName, 
          validState:item.validState
        };
        if(newIndex){
          indexObj.displayState = 'y';
        }else{
          indexObj.displayState = !item.isOnView?'y':'n';
        }
        tempParam.push(indexObj);
      });

      angular.forEach(viewObj.indexs,function(item){
        var indexObj = {
          systemCode:item.systemCode,
          systemName:item.systemName,
          indexName:item.indexName,
          indexDisplayName:item.indexDisplayName,
          displayState:'n',
          validState:item.validState
        };
        tempParam.push(indexObj);
      });     

      CoreLoading.setState(true);
      TradeLogManageServer.indexModify({indexConfs:tempParam},function(res){
        if(res.state === '10000'){
          viewObj.series = [];
          viewObj.indexs = [];
          angular.forEach(tempParam,function(item){
            if(item.displayState === 'y'){
              viewObj.series.push(item);
            }else{
              viewObj.indexs.push(item);
            }
          });
          CoreLoading.setState(false);
          CoreService.dialog('保存成功');
        }else{
          CoreLoading.setState(false);
          CoreService.dialog('10003');
        }
      });
    }
  }

  tradeLogManageViewObj.$inject = ['TradeLogManageServer','CoreService','CoreLoading'];

})();